{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "AugLy_audio.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3",
      "language": "python"
    },
    "language_info": {
      "name": "python",
      "version": "3.8.5-final"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "73El6nZhlFBv"
      },
      "source": [
        "# Note: restart runtime after this import before running the augmentations\n",
        "!pip install -U augly[audio]\n",
        "!sudo apt-get install python3-magic"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DKzam1fDlZvj"
      },
      "source": [
        "import os\n",
        "import augly.audio as audaugs\n",
        "import augly.utils as utils\n",
        "from IPython.display import display, Audio\n",
        "\n",
        "# Get input audio\n",
        "input_audio = os.path.join(\n",
        "    utils.TEST_URI, \"audio\", \"inputs\", \"vad-go-stereo-44100.wav\"\n",
        ")\n",
        "display(Audio(input_audio))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EcfYFx2zlbVf"
      },
      "source": [
        "# Now we can apply various augmentations to the audio!\n",
        "aug_audio, sr = audaugs.pitch_shift(input_audio, n_steps=4.0)\n",
        "display(Audio(aug_audio, rate=sr))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j5C8mlNMleDa"
      },
      "source": [
        "\"\"\"\n",
        "You can optionally pass in a metadata list, to which metadata about the\n",
        "augmentation will be appended, including kwargs, the durations & number of\n",
        "channels of the input & output audio clips, the matching segments in the\n",
        "input/output audio clips (useful in case of temporal editing), and intensity\n",
        "(defined based on the kwargs for each augmentation).\n",
        "\"\"\"\n",
        "meta = []\n",
        "aug_audio, sr = audaugs.time_stretch(\n",
        "    input_audio,\n",
        "    rate=0.5,\n",
        "    metadata=meta,\n",
        ")\n",
        "display(Audio(aug_audio, rate=sr))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UgaXuNwf5Ldn"
      },
      "source": [
        "# For all the augmentations, we have class-based definitions as well as\n",
        "# functional\n",
        "from augly.audio.utils import validate_and_load_audio\n",
        "\n",
        "input_audio_arr, sr = validate_and_load_audio(input_audio)\n",
        "meta = []\n",
        "aug = audaugs.PeakingEqualizer()\n",
        "aug_audio, sr = aug(input_audio_arr, sample_rate=sr, metadata=meta)\n",
        "display(Audio(aug_audio, rate=sr))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "P6711hrMaj_h"
      },
      "source": [
        "# You can also compose several transformations together\n",
        "meta = []\n",
        "aug = audaugs.Compose(\n",
        "    [\n",
        "        audaugs.AddBackgroundNoise(),\n",
        "        audaugs.ToMono(),\n",
        "        audaugs.Clicks(),\n",
        "    ]\n",
        ")\n",
        "aug_audio, sr = aug(input_audio_arr, sample_rate=sr, metadata=meta)\n",
        "display(Audio(aug_audio, rate=sr))\n",
        "meta"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}